# Maintainer: Antheas Kapenekakis <aur at antheas dot dev>
pkgname=hhd
_pkgname=hhd
pkgver=2.5.3
pkgrel=1
pkgdesc='Handheld Daemon. A tool for managing the quirks of handheld devices.'
arch=('x86_64')
url='https://github.com/hhd-dev/hhd'
license=('MIT')
depends=('python' python-setuptools 'python-evdev' 'python-rich' 'python-yaml')
provides=('hhd')
conflicts=('hhd')
optdepends=('hhd-user: allows running hhd as a user service.')
makedepends=('python-'{'build','installer','setuptools','wheel','babel'})
source=("git+https://gitee.com/honjow/hhd.git")
sha512sums=('SKIP')

prepare() {
  # patch
  cd "$srcdir/$_pkgname"

  echo "Preparing..."

  local src
  for src in "${source[@]}"; do
    src="${src%%::*}"
    src="${src##*/}"
    [[ $src = *.patch ]] || continue
    echo "Applying patch $src..."
    patch -Np1 < "../$src"
  done

}

build() {
  cd "$srcdir/$_pkgname"
  pybabel compile -D hhd -d ./i18n
  pybabel compile -D adjustor -d ./i18n
  cp -R ./i18n/* ./src/hhd/i18n
  python -m build --wheel --no-isolation
}

pkgver() {
    cd "$srcdir/$_pkgname"
    git config advice.detachedHead false
    tag=$(git describe --tags --abbrev=0)
    git checkout $tag
    _pkgver=$(echo $tag | sed 's/^v//')
    printf "%s" "$_pkgver"
}

package() {
  cd "$srcdir/$_pkgname"

  python -m installer --destdir="$pkgdir" dist/*.whl
  
  # Install minimally necessary rules for running as a system service
  mkdir -p ${pkgdir}/usr/lib/udev/rules.d/
  install -m644 usr/lib/udev/rules.d/83-hhd.rules ${pkgdir}/usr/lib/udev/rules.d/83-hhd.rules
  mkdir -p ${pkgdir}/usr/lib/udev/hwdb.d/
  install -m644 usr/lib/udev/hwdb.d/83-hhd.hwdb ${pkgdir}/usr/lib/udev/hwdb.d/83-hhd.hwdb
  mkdir -p ${pkgdir}/usr/lib/systemd/system/
  install -m644 usr/lib/systemd/system/hhd@.service ${pkgdir}/usr/lib/systemd/system/hhd@.service
}
